<?php

namespace App\Http\Controllers\Wechat\Plugins;

use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Controllers\Wechat\Messages;
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Message;


class Register extends BaseController
{
    /**
     * 绑定
     * @param $message
     * @return \EasyWeChat\Message\Text
     */
    public static function binding($message)
    {
        $username = substr($message->Content, 6, 10);
        $password = substr($message->Content, 16);

        $isValid = \App\Library\User::isValid($username, $password);

        if (!$isValid) {
            return Messages\Text::wrongRegister();
        }

        $realname  = '';
        $dlpuEcard = (new \Xu42\DlpuEcard\DlpuEcard($username))->getBalance();
        if (@$dlpuEcard['status'] == 200) {
            $realname = $dlpuEcard['data']['name'];
        }

        $isRegister = \App\Library\User::isRegister($username);

        $result = \App\Library\User::saveOrUpdate($isRegister, $username, $realname, $password, $message->FromUserName,
            '');
        return Messages\Text::simple($result);
    }


    /**
     * 校园网自护服务账号绑定
     * @param $message
     * @return \EasyWeChat\Message\Text
     */
    public static function bindingNetwork($message)
    {
        $user     = explode(" ", $message->Content);
        $username = trim($user[1]);
        $password = trim($user[2]);

        $isValid = Network::isValid($username, $password);

        if (!$isValid) {
            return Messages\Text::wrongRegister();
        }

        $realname  = '';

        $isRegister = Network::isRegister($username);

        $result = Network::saveOrUpdate($isRegister, $username, $realname, $password, $message->FromUserName, '');

        return Messages\Text::simple($result);
    }

}